import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect:'/login'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import("@/views/login/MyLogin.vue"),
  },
  {
    path: '/myLayout',
    name: 'myLayout',
    redirect: '/myIndex',
    component: () => import("@/layout/MyLayout.vue"),
    children: [
      {
        path: '/myIndex',
        name: 'myIndex',
        component: () => import("@/views/MyIndex.vue"),
      },
      {
        path: '/worksType',
        name: 'worksType',
        component: () => import("@/views/works/WorksType.vue"),
      },
      {
        path: '/worksList',
        name: 'worksList',
        component: () => import("@/views/works/WorksList.vue"),
      },
      {
        path: '/diaryType',
        name: 'diaryType',
        component: () => import("@/views/diary/DiaryType.vue"),
      },
      {
        path: '/diaryList',
        name: 'diaryList',
        component: () => import("@/views/diary/DiaryList.vue"),
      },
      {
        path: '/personalInfo',
        name: 'personalInfo',
        component: () => import("@/views/system/PersonalInfo.vue"),
      },
      {
        path: '/projectList',
        name: 'projectList',
        component: () => import("@/views/project/ProjectList.vue"),
      }
    ]
  }
]


const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 路由拦截  在跳转前执行
router.beforeEach(function (to, from, next) {
  if (!localStorage.getItem('user')) {
      if (to.path != '/login') {
          next('/login')
      }
  }
  next()
})

export default router
